/**
 * 
 */
package com.mc.file;

import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.apache.commons.io.FileUtils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.mc.parameter.Parameter;

import cn.hutool.core.io.FileUtil;

/**
 * @author w.weizhen
 *
 */
public class PlatformTest {

	/**
	 * @param args
	 */
	public static void main(String[] args) 
	{
		read();
		
		getCode();
	}
	
	private static final String path = Parameter.data_path + "platform" + File.separator;
	private static void getCode()
	{
		File original = new File(path, "original");
		if (original.exists()) 
        {
            File[] files = original.listFiles();
            for (File file : files) {
                String name = file.getName();
                if(file.isFile() && name.indexOf("-hs-platform") > -1 && name.indexOf(".log") > -1)
                {
                	sorting(file);
                }
            }
        }
		
		save();
	}
	
	//分解处理文件
	private static final String log_mark = "TaskYlfwShanghaiApiOut.wasteTimedTask: warn when collect.";
	private static void sorting(File file)
	{
		List<String> lines = FileUtil.readLines(file, "UTF-8");
		for (int i = 0; i < lines.size(); i++) 
		{
			String line = lines.get(i);
			if(line.indexOf(log_mark) > -1)
			{
				line(line);
			}
		}
	}
	
	//分解处理行
	private static final List<String> starts = Arrays.asList("orgCode=",", flowNo=",", medKey=");
	private static final List<String> ends = Arrays.asList(", flowNo=",", medKey=",", districtCode=");
	private static List<String> orgCodes = new ArrayList<String>();
	private static List<String> flowNos = new ArrayList<String>();
	private static List<String> medKeys = new ArrayList<String>();
	private static void line(String line)
	{
		String orgCode = line.substring(line.indexOf(starts.get(0)) + starts.get(0).length(), line.indexOf(ends.get(0)));
		String flowNo = line.substring(line.indexOf(starts.get(1)) + starts.get(1).length(), line.indexOf(ends.get(1)));
		String medKey = line.substring(line.indexOf(starts.get(2)) + starts.get(2).length(), line.indexOf(ends.get(2)));
		if(!orgCodes.contains(orgCode) || !flowNos.contains(flowNo) || !medKeys.contains(medKey))
		{
			orgCodes.add(orgCode);
			flowNos.add(flowNo);
			medKeys.add(medKey);
		}
	}
	
	private static String output = path + "output" + File.separator;
	private static void save()
	{
		try {
			FileUtils.writeStringToFile(new File(output, "orgCodes.txt"), orgCodes.toString(), "UTF-8");
			FileUtils.writeStringToFile(new File(output, "flowNos.txt"), flowNos.toString(), "UTF-8");
			FileUtils.writeStringToFile(new File(output, "medKeys.txt"), medKeys.toString(), "UTF-8");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	
	private static void read()
	{
		try {
			FileUtils.writeStringToFile(new File(output, "orgCodes.txt"), orgCodes.toString(), "UTF-8");
			FileUtils.writeStringToFile(new File(output, "flowNos.txt"), flowNos.toString(), "UTF-8");
			FileUtils.writeStringToFile(new File(output, "medKeys.txt"), medKeys.toString(), "UTF-8");
			
			File orgCodesFile = new File(output + "orgCodes.txt");
			if(orgCodesFile.exists() && orgCodesFile.isFile())
			{
				JSONArray orgCodesJSONArray = JSON.parseArray(FileUtils.readFileToString(orgCodesFile, "UTF-8"));
				orgCodes = orgCodesJSONArray.toJavaList(String.class);
			}
			File flowNosFile = new File(output + "flowNos.txt");
			if(flowNosFile.exists() && flowNosFile.isFile())
			{
				JSONArray flowNosJSONArray = JSON.parseArray(FileUtils.readFileToString(flowNosFile, "UTF-8"));
				flowNos = flowNosJSONArray.toJavaList(String.class);
			}
			File medKeysFile = new File(output + "orgCodes.txt");
			if(medKeysFile.exists() && medKeysFile.isFile())
			{
				JSONArray medKeysJSONArray = JSON.parseArray(FileUtils.readFileToString(medKeysFile, "UTF-8"));
				medKeys = medKeysJSONArray.toJavaList(String.class);
			}
			
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}